<?php

class Tavs_Image
{
	private $_fileName;
	
	private $_attribs = array();
	
	private $_mime;
	
	private $_view;
	
	public function __construct($fileName)
	{
		$this->setFileName($fileName);
		if ($this->_fileExists()) {
			$this
				->setAttrib('src', $this->_fileName)
				->_readImageInfo();
		}
	}
	
	private function _readImageInfo()
	{
		$info = @getimagesize($this->_fileName);
		
		$this->_mime = $info['mime'];
		
		$this
			->setWidth($info[0])
			->setHeight($info[1]);
		
		return $this;
	}
	
	private function _normalizeAttribName($attrib)
	{
		$attrib = strtolower((string) $attrib);
		return $attrib;
	}
	
	
	public function setFileName($fileName)
	{
		$this->_fileName = (string) $fileName;
		return $this;
	}
	
	public function getFileName($appendBaseUrl = false)
	{
		$baseUrl = '';
		if (true === (bool) $appendBaseUrl) {
			require_once 'Zend/Controller/Front.php';
			$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl() . '/';
		}
		return $baseUrl . $this->_fileName;
	}
	
	public function setWidth($width)
	{
		$this->_attribs['width'] = (int) $width;
		return $this;
	}
	
	public function setHeight($height)
	{
		$this->_attribs['height'] = (int) $height;
		return $this;
	}
	
	public function setAttrib($attrib, $value)
	{
		$attrib = $this->_normalizeAttribName($attrib);
		
		if ('src' == $attrib && isset($this->_attribs['src'])) {
			throw new Tavs_Image_Exception('Attribute src was be setted before');
		}
		else {
			$this->_attribs[$attrib] = (string) $value;
		}
		return $this;
	}
	
	public function setAttribs(array $attribs)
	{
		foreach ($attribs as $attrib => $value) {
			$this->setAttrib($attrib, $value);
		}
		
		return $this;
	}
	
	public function removeAttrib($attrib)
	{
		$attrib = (string) $attrib;
		if (isset($this->_attribs[$attrib])) {
			unset($this->_attribs[$attrib]);
		}
	}
	
	private function _fileExists($throw = true)
	{
		if ( file_exists($this->_fileName) ) {
			return true;		
		}
		elseif (true === (bool) $throw) {
			throw new Tavs_Image_Exception($this->_fileName . ' not found');
		}
		else {
			return false;
		}
	}
	
	public function setView(Zend_View_Interface $view)
	{
		$this->_view = $view;
		return $this;
	}
	
	/**
	 * Retorna o objeto de renderizacao
	 *
	 * @return Zend_View_Interface
	 */
	public function getView()
	{
		if ( null === $this->_view )
        {
        	/**
        	 * @see Zend_Controller_Action_HelperBroker
        	 */
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            $this->setView($viewRenderer->view);
        }

        return $this->_view;
	}
	
	public function render()
	{
		$view = $this->getView();
		
		$attribs = $this->_attribs;
		$src = $this->getFileName(true);
		unset($attribs['src']);
		
		$xhtml = $view->htmlImage($src, $attribs, false);
		
		return $xhtml;
	}
	
	public function __set($name, $value)
	{
		$this->setAttrib($name, $value);		
	}
	
	public function __unset($name)
	{
		$this->removeAttrib($name);
	}
	
	public function __toString()
	{
		try {
			$content = $this->render();
			return $content;
		}
		catch (Exception $e) {
			trigger_error($e->getMessage(), E_USER_ERROR);
			return '';
		}
	}
	
}